<?php

namespace app\admin\controller;
use think\Db;

class Service
{
    public function __construct()
    {
        Admin_auth();
    }

    public function index()
    {
        $uid = input('get.uid');
        $title = input('get.title');
        $where = [];
        if (!empty($uid)) {
            $where['uid'] = $uid;
        }
        if (!empty($title)) {
            $where['title'] = ['like', '%' . $title . '%'];
        }
        $pages = pages('service', $where, input('get.page'));
        $data = Db::table("service")->whereIn('aid', session('aid'))->where($where)->limit($pages['limit'])->order("replace(flag,'0','desc'),id desc")->select();
        return view('', ['data' => $data, 'pages' => $pages['pages']]);
    }

    public function edit()
    {
        $id = input("get.id") ? input("get.id") : 0;
        if (request()->isPost()) {
            $data = request()->post();
			if($data['spec']){
				$spec = $data['spec'];
			}
			if($data['start_time'] && $data['end_time']){
				$data['start_time'] = strtotime($data['start_time']);
				$data['end_time'] = strtotime($data['end_time']);
			}
			unset($data['spec']);
            $seller = Db::table("seller")->where(['id' => $data['uid']])->whereIn('aid', session('aid'))->find();
            $data['pid'] = $seller['pid'];
            $data['cid'] = $seller['cid'];
            $data['aid'] = $seller['aid'];
			$new_spec = array();
            if ($id) {
                Db::table("service")->whereIn('aid', session('aid'))->where(['id' => $id])->update($data);
            } else {
                $data['status'] = 1;
                $data['time'] = time();
                $id = Db::table("service")->insertGetId($data);
            }
			$ids = array();
			$speclist = Db::table("service_spec")->field('id')->where(['service_id' => $id])->select();
			foreach ($speclist as $v) $ids[] = $v['id'];
			if(is_array($spec)){
				foreach ($spec as $sp) {
					if (empty($sp['title'])) continue;
					$f_data = array(
						'title' => $sp['title'],
						'service_id' => $id,
						'price' => $sp['price']
					);
					if ($sp['id'] && in_array($sp['id'], $ids)) {
						Db::table("service_spec")->where(['id' => $sp['id']])->update($f_data);
						$ids = array_diff($ids, array($sp['id']));
					} else {
						Db::table("service_spec")->insert($f_data);
					}
				}
			}
			if ($ids) {
				Db::table("service_spec")->where(['service_id' => $id])->delete($ids);
			}
        }
        $data = Db::table("service")->whereIn('aid', session('aid'))->where(['id' => $id])->find();
		$speclist = Db::table("service_spec")->where(['service_id' => $id])->select();
		// var_dump($speclist);exit;
        $seller = Db::table("seller")->whereIn('aid', session('aid'))->where(['status' => 1])->order('id desc')->select();
        return view('', ['data' => $data, 'seller' => $seller, 'speclist' => $speclist]);
    }

    public function del()
    {
        $id = input("get.id") ? input("get.id") : 0;
        $page = input("get.page");
        Db::table("service")->whereIn('aid', session('aid'))->delete($id);
        Db::table("volume")->where('goods_id',$id)->delete();
        Db::table("service_spec")->where('service_id',$id)->delete();
        redirect(url('/admin/service') . '?page=' . $page);
        exit;
    }

    public function flag()
    {
        $id = input("get.id");
        $flag = input("get.flag");
        $flagtime = input("get.flagtime");
        if (!$id) {
            $return = array(
                'code' => 'error',
                'msg' => "id" . lang('currency')['error']
            );
            return $return;
        }
        if ($flagtime) {
            $flagtime = strtotime($flagtime) + 86399;
        }
        $update['flag'] = $flag;
        $update['flagtime'] = $flagtime;
        Db::table("service")->where(['id' => $id])->whereIn('aid', session('aid'))->update($update);
        $return = array(
            'code' => 'success',
            'msg' => lang('currency')['success']
        );
        return $return;
    }

    public function examine()
    {
        $pages = pages('service', ['status' => 3], input('get.page'));
        $data = Db::table("service")->where(['status' => 3])->whereIn('aid', session('aid'))->limit($pages['limit'])->order('id desc')->select();
        return view('', ['data' => $data, 'pages' => $pages['pages']]);
    }

    public function setting()
    {
        if (request()->isPost()) {
            $data = request()->post();
            Db::table("seller_config")->where(['id' => 1])->update($data);
        }
        $data = Db::table("seller_config")->where(['id' => 1])->find();
        return view('', ['data' => $data]);
    }

    public function category()
    {
        $uid = request()->request('uid');
        $where = [];
        if ($uid) {
            $where['uid'] = $uid;
        }
        $data = Db::table("service_category")->whereIn('aid', session('aid'))->where($where)->order('id asc')->select();
        foreach ($data as $k => $v) {
            $data[$k]['seller'] = Db::table('seller')->where('id', $v['uid'])->value('name');
        }
        return view('', ['data' => $data]);
    }

    public function upload()
    {
        $config = Db::table('material_config')->where('id', 1)->find();
        config([
            'UPLOAD_SITEIMG_QINIU' => [
                'maxSize' => 5 * 1024 * 1024,//文件大小
                'rootPath' => './',
                'saveName' => ['uniqid', ''],
                'driver' => 'Qiniu',
                'driverConfig' => [
                    'secretKey' => $config['secretKey'],
                    'accessKey' => $config['accessKey'],
                    'domain' => $config['domain'],
                    'bucket' => $config['bucket'],
                ],
            ],
            'file_size' => $config['file_size']
        ]);
        $setting = config('UPLOAD_SITEIMG_QINIU');
        $Upload = new \think\Upload($setting);
        $infos = $Upload->upload($_FILES);
        return $infos;
    }

    public function category_edit()
    {
        $id = input("get.id") ? input("get.id") : 0;
        if (request()->isPost()) {
            $data = request()->post();
            if (array_key_exists('uid', $data)) {
                if ($data['uid'] == '') {
                    echo '请选择商户';
                    return view('', ['data' => $data]);
                }
            } else {
                echo '请选择商户';
                return view('', ['data' => $data]);
            }
            $seller = Db::table('seller')->where('id', $data['uid'])->find();
            $data['pid'] = $seller['pid'];
            $data['cid'] = $seller['cid'];
            $data['aid'] = $seller['aid'];
            if ($_FILES['image']['tmp_name'] != '') {
                $image = $this->upload();
                $data['image'] = $image['image']['url'];
            }
            if (!empty($id)) {
                Db::table("service_category")->where('id', $id)->update($data);
            } else {
                Db::table("service_category")->insert($data);
            }
            echo 'please wait...';
            return '<script>parent.location.reload();</script>';
        } else {
            $data = [];
            if (!empty($id)) {
                $data = Db::table('service_category')->where('id', $id)->find();
            }
            error_reporting(0);
            return view('', ['data' => $data]);
        }
    }

    public function category_flag()
    {
        $id = input("get.id") ? input("get.id") : 0;
        $flag = input('get.flag');
        if ($flag == 1) {
            $flag = 0;
        } else {
            $flag = 1;
        }
        Db::table('service_category')->where('id', $id)->update(['flag' => $flag]);
        $return = array(
            'code' => 'success',
            'msg' => lang('currency')['success']
        );
        return $return;
    }

    public function flag_edit()
    {
        $id = input("get.id") ? input("get.id") : 0;
        if (request()->isPost()) {
            $data = request()->post();
            if ($_FILES['image']['tmp_name'] != '') {
                $flag_image = $this->upload();
                $data['flag_image'] = $flag_image['image']['url'];
            }
            Db::table('service_category')->where('id', $id)->update($data);
            echo 'please wait...';
            return '<script>parent.location.reload();</script>';
        } else {
            $data = Db::table('service_category')->where('id', $id)->find();
            return view('', ['data' => $data]);
        }
    }

    public function category_del()
    {
        $id = input("get.id") ? input("get.id") : 0;
        Db::table("service_category")->delete($id);
        $return = array(
            'code' => 'success',
            'msg' => lang('currency')['success']
        );
        return $return;
    }

    public function get_seller()
    {
        $data = Db::table("seller")->whereIn('aid', session('aid'))->order('id asc')->select();
        $return = array(
            'data' => $data
        );
        return $return;
    }

    public function ajax_get()
    {
        $where = [];
        $aid = request()->request('aid');
        $uid = request()->request('uid');
        if ($aid) {
            $where['aid'] = $aid;
        }
        if ($uid) {
            $where['uid'] = $uid;
        }
        $data = Db::table("service_category")->whereIn('aid', session('aid'))->where($where)->order('id asc')->select();
        $return = array(
            'data' => $data
        );
        return $return;
    }
}
